<script setup lang="ts">
  import { ref } from 'vue'
  import { onLoad } from '@dcloudio/uni-app'
  import {
    playCheck,
    productCancel,
    productOrder,
    productPlay
  } from '@/api/training'
  import { SIGN_ENUM, SIGN_ENUM_TEXT } from '@/utils/basicData'
  import { msg, wechatPayment } from '@/utils/tools'
  const productsInfo = ref({})
  const totalPrice = ref(0)
  const registrationId = ref()
  const bottomPadding = (uni.getSystemInfoSync().screenHeight as number) - (uni.getSystemInfoSync().safeArea.bottom as number)
  onLoad((options:any) => {
    registrationId.value = options.id
    getInfoData(registrationId.value)
  })

  const getInfoData = (registrationId:any) => {
    productOrder({registrationId}).then(res => {
      productsInfo.value = res.data
      calculateAmount()
    })
  }

  const calculateAmount = () => {
    totalPrice.value = 0
    productsInfo.value.products.map((item:any) => {
      totalPrice.value += +item.productAmount
    })
    if(totalPrice.value !== 0){
      totalPrice.value = totalPrice.value * 0.01
    }
  }

  const handlerPreOrder = () => {
    productPlay({ orderId:productsInfo.value.orderId }).then((res) => {
      // 起调支付
      wechatPayment(res.data.payInfo).then((resData:any) =>{
        // 支付后检查是否支付
        playCheck({orderId:res.data.orderId}).then(_res => {
          msg('支付成功')
          const time = setTimeout(() => {
            uni.redirectTo({url:`/pageSignUp/trainOrder/trainOrder?id=${registrationId.value}`})
          },500)
        }).catch((err:any) => {
          msg('检查失败，请从新进行页面')
        })
      }).catch((err:any) => {
        msg('支付失败，请重试')
      })
    })
  }

  // 取消订单
  const handlerCancel = () => {
    productCancel({orderId:productsInfo.value.orderId}).then(res => {
      if(res.data){
        msg('取消成功')
        const time = setTimeout(() => {
          uni.navigateBack({delta:1})
          clearTimeout(time)
        },500)
      }
    })
  }

</script>

<template>
  <view class="order-body">
    <xx-title title="附加产品订单"/>
    <view class="order-item">
      <view class="order-title">附加订单状态</view>
      <view class="order-status origin_text" >待支付</view>
    </view>

    <view class="order-item">
      <view class="order-header">
        <view class="order-name">附加产品</view>
        <view class="order-num">共计{{ productsInfo.products?.length }}个服务</view>
      </view>
      <view class="order-body-item" v-for="item in productsInfo.products" :key="item.id">
        <view class="body-list-name">{{ item.name }}</view>
        <view class="body-item-bottom">
          <view class="body-item-left">
            <view class="left-info">价格：￥{{item.productAmount * 0.01}}</view>
          </view>
        </view>
      </view>
      <view class="order-body-btn">
        <text>合计：</text>
        <view class="money">￥{{totalPrice}}</view>
      </view>
    </view>

    <view class="order-item">
      <view class="order-header">
        <view class="order-name">学员信息</view>
      </view>
      <view class="order-info">
        <view class="order-info-item">
          <view class="label">身份证</view>
          <view class="value">{{productsInfo.idCardMask}}</view>
        </view>
        <view class="order-info-item">
          <view class="label">联系人</view>
          <view class="value">{{ productsInfo.name }}</view>
        </view>
        <view class="order-info-item" v-if="productsInfo.mobile">
          <view class="label">联系电话</view>
          <view class="value">{{productsInfo.mobile}}</view>
        </view>
      </view>
    </view>
    <view class="bottom-fixed" :style="{paddingBottom:`calc(${bottomPadding}px + 14rpx)`}">
      <view class="seizeASeat"></view>
    </view>
    <view class="bottom-btn" :style="{paddingBottom:`calc(${bottomPadding}px + 14rpx)`}">
      <view class="btn" @click="handlerCancel">取消订单</view>
      <view class="btn group-registration" @click="handlerPreOrder">立即支付</view>
    </view>
  </view>
</template>

<style scoped lang="scss">
  .order-body{
    width: 100%;
    box-sizing: border-box;
    padding: 1rpx;
    background-color: #F5F5F5;
    min-height: 100vh;
    .order-item{
      margin: 28rpx;
      background-color: #FFFFFF;
      border-radius: 16rpx;
      padding: 32rpx;
      .order-title{
        font-size: 24rpx;
        color: #818496;
      }
      .order-status{
        margin-top: 5rpx;
        font-size: 36rpx;
        font-weight: 500;
      }
      .order-header{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-bottom: 14rpx;
        border-bottom: 1rpx solid #EBEBEB;
        .order-name{
          font-size: 32rpx;
          color: #21252E;
        }
        .order-num{
          font-size: 24rpx;
          color: #818496;
        }
      }
      .order-body-item{
        padding: 14rpx 0;
        border-bottom: 1rpx solid #F5F5F5;
        .body-list-name{
          font-size: 28rpx;
          color: #21252E;
          margin-bottom: 4rpx;
        }
        .body-item-bottom{
          display: flex;
          justify-content: space-between;
          align-items: center;
          .body-item-left{
            width: calc(100% - 150rpx);
            .left-info{
              font-size:24rpx;
              color: #505363;
            }
            .left-num{
              margin-top: 5rpx;
              font-size: 24rpx;
              color: #505363;
            }
          }
          .body-item-right{
            width: 150rpx;
            .btn{
              font-size: 24rpx;
              padding: 8rpx 24rpx;
              border-radius: 24rpx;
              border: 1rpx solid #ECEEF1;
            }
          }
        }
      }
      .order-body-btn{
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding-top: 24rpx;
        text{
          font-size: 24rpx;
          color: #818496;
        }
        .money{
          color: #F37C2B;
          font-size: 32rpx;
          font-weight: 500;
        }
      }
      .order-info{
        padding: 10rpx 0;
        .order-info-item{
          display: flex;
          justify-content: space-between;
          align-items: center;
          padding: 8rpx 0;
          font-size: 28rpx;
          .label{
            color: #505363;
          }
          .value{
            color: #505363;
          }
        }
      }
      .order-info-item-center{
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 24rpx;
        color: #999999;
        margin-top:10rpx;
      }
    }
  }

  .bottom-fixed{
    padding-top: 14px;
    background-color: #F5F5F5;
    .seizeASeat{
      height: 60rpx;
    }
  }
  .bottom-btn{
    position: fixed;
    bottom: 0;
    width: 100%;
    display: flex;
    justify-content: flex-end;
    align-items: center;
    padding-left: 28rpx;
    padding-right: 28rpx;
    box-sizing: border-box;
    padding-top: 14rpx;
    background-color: #FFFFFF;
    .btn{
      padding: 16rpx 32rpx;
      margin-right:10rpx;
      text-align: center;
      border-radius: 100rpx;
      box-sizing: border-box;
      border:1rpx solid #92C75B;
      font-size: 28rpx;
      font-weight: 500;
      letter-spacing: 2rpx;
      &:last-child{
        margin-right: 0;
      }
    }
    .group-registration{
      background: #92C75B;
      color: #FFFFFF;
    }

  }
  .insurance{
    margin:28rpx;
    background-color: #FFFFFF;
    border-radius: 16rpx;
    padding: 32rpx;
    .title{
      font-size: 32rpx;
      color: #21252E;
      display: flex;
      align-items: center;
      padding-bottom: 14rpx;
      border-bottom: 1rpx solid #EBEBEB;
      &:before{
        content: '*';
        color: #f34848;
      }
    }
    .insurance-file{
      padding-top: 14rpx;
    }
  }

  .service-list{
    padding: 10rpx;
    .service-item{
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20rpx;
      border-radius: 20rpx;
      background-color: rgba(243, 124, 43, 0.06);
      .service-body{
        width: calc(100% - 100rpx);
        .service-title{
          font-size: 28rpx;
          color: #505363;
        }
        .service-content{
          margin-top: 5rpx;
          font-size: 24rpx;
          color: #818496;
        }
      }
      .service-btn{
        width:100rpx;
        .btn{
          margin: auto 0;
          padding: 14rpx 20rpx;
          border-radius: 10rpx;
          background-color: #F36E13;
          color:#ffffff;
          font-size: 28rpx;
          text-align: center;
        }
      }
    }
  }
</style>
